var t = getApp(), a = require("../../A4730AC3A71FB5DFC21562C4223CA4D1.js");

Page({
    data: {
        winHeight: t.globalData.windowHeight,
        winWidth: t.globalData.windowWidth,
        statusBarHeight: t.globalData.statusBarHeight,
        titleBarHeight: t.globalData.titleBarHeight,
        classify: [],
        classifyObj: {
            0: {
                classify_id: 0,
                classify_title: "全部类别"
            }
        },
        curClassify: -1,
        sortObj: {
            _: {
                label: "默认排序",
                sort_name: "",
                sort: ""
            },
            "1_0": {
                label: "价格最低",
                sort_name: "1",
                sort: "0"
            },
            "1_1": {
                label: "价格最高",
                sort_name: "1",
                sort: "1"
            },
            "2_1": {
                label: "新品",
                sort_name: "2",
                sort: "1"
            }
        },
        sortList: [],
        curSort: 1,
        curSortName: 2,
        list: [],
        total: 0,
        leftList: [],
        rightList: [],
        sortType: 2,
        isShowMask: !1,
        isLoading: !1,
        page: 1
    },
    onLoad: function() {
        this.getSortList(), this.getPictureList({
            page: 1,
            sort_name: 1 * this.data.curSortName,
            sort: 1 * this.data.curSort
        }, !1, !0);
    },
    onReady: function() {
        this.sortAnim = wx.createAnimation({
            duration: 200
        }), this.classifyAnim = wx.createAnimation({
            duration: 200
        });
    },
    onShow: function() {
        this.data.classify.length || this.getProductClassify();
    },
    onShareAppMessage: function() {},
    getSortList: function() {
        var t = this.data.sortObj, a = [];
        for (var i in t) "2_1" !== i && a.push(t[i]);
        this.setData({
            sortList: a
        });
    },
    onListScrollBottom: function() {
        var t = {
            page: this.data.page + 1
        };
        this.data.curSortName && (t.sort_name = 1 * this.data.curSortName, t.sort = 1 * this.data.curSort), 
        this.data.curClassify > 0 && (t.classify_id = this.data.curClassify), this.getPictureList(t);
    },
    getProductClassify: function() {
        var t = this;
        a.get("/v2/picture/productClassify").then(function(a) {
            if (1001 === a.data.code) {
                var i = [ {
                    classify_id: 0,
                    classify_title: "全部类别"
                } ].concat(a.data.response.data), s = {}, r = !0, o = !1, e = void 0;
                try {
                    for (var n, c = i[Symbol.iterator](); !(r = (n = c.next()).done); r = !0) {
                        var d = n.value;
                        s[d.classify_id] = d;
                    }
                } catch (t) {
                    o = !0, e = t;
                } finally {
                    try {
                        !r && c.return && c.return();
                    } finally {
                        if (o) throw e;
                    }
                }
                t.setData({
                    classify: i,
                    classifyObj: s
                });
            }
        });
    },
    getPictureList: function() {
        var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, i = this, s = arguments[1], r = arguments[2];
        !s && this.data.isLoading || (r && wx.showLoading({
            title: "加载中...",
            mask: !0
        }), this.setData({
            isLoading: !0
        }), a.get("/v2/picture/product", t).then(function(a) {
            if (r && wx.hideLoading(), i.setData({
                isLoading: !1
            }), 1001 === a.data.code) {
                var o = i.data.list, e = a.data.response, n = {
                    list: o = t.page > 1 ? o.concat(e.data) : e.data,
                    total: e.total,
                    page: t.page
                };
                s && (n.leftList = [], n.rightList = []), i.setData(n, function() {
                    (e.data.length || s) && i.formatProduct();
                });
            }
        }).catch(function() {
            r && wx.hideLoading(), i.setData({
                isLoading: !1
            });
        }));
    },
    formatProduct: function() {
        var t = this.data.list, a = 0, i = 0, s = [], r = [], o = !0, e = !1, n = void 0;
        try {
            for (var c, d = t[Symbol.iterator](); !(o = (c = d.next()).done); o = !0) {
                var h = c.value, u = h.product_price_pattern_cent, l = h.product_coverimg.width, f = h.product_coverimg.height, g = JSON.parse(JSON.stringify(h));
                g.product_price_pattern_cent = 0 === u ? u : Math.round(u / 100), g.product_coverimg.width = 320, 
                g.product_coverimg.height = 320 * f / l || 0, a <= i ? (a += g.product_coverimg.height + 176, 
                s.push(g)) : (i += g.product_coverimg.height + 176, r.push(g));
            }
        } catch (t) {
            e = !0, n = t;
        } finally {
            try {
                !o && d.return && d.return();
            } finally {
                if (e) throw n;
            }
        }
        this.setData({
            leftList: s,
            rightList: r
        });
    },
    navToDetail: function(t) {
        wx.navigateTo({
            url: "/pages/picture/index?id=" + t.currentTarget.dataset.id
        });
    },
    handleSortType: function(t) {
        var a = this, i = 1 * t.currentTarget.dataset.type;
        if (2 === i) 2 !== this.data.sortType && ("2" !== this.data.curSortName ? this.setData({
            sortType: 2,
            curClassify: -1,
            page: 1,
            curSort: "1",
            curSortName: "2"
        }, function() {
            a.handleSortAnim(!0), a.getPictureList({
                sort_name: 2,
                page: 1,
                sort: 1
            }, !0);
        }) : this.setData({
            sortType: 2
        }, function() {
            a.handleSortAnim(!0);
        })); else {
            var s = this.data.sortType === i && this.data.isShowMask;
            this.setData({
                sortType: i
            }, function() {
                s ? a.hideSortList() : a.handleSortAnim(s);
            });
        }
    },
    handleSortAnim: function(t) {
        var a = this.data.sortType, i = {
            isShowMask: !t
        };
        t ? (this.sortAnim.bottom(0).step({
            duration: 200
        }), this.classifyAnim.bottom(0).step({
            duration: 200
        })) : 1 === a ? (this.sortAnim.bottom(-this.data.winWidth / 750 * 300).step({
            duration: 200
        }), this.classifyAnim.bottom(0).step({
            duration: 0
        })) : 3 === a && (this.sortAnim.bottom(0).step({
            duration: 0
        }), this.classifyAnim.bottom(-this.data.winWidth).step({
            duration: 200
        })), i.sortAnimData = this.sortAnim.export(), i.classifyAnimData = this.classifyAnim.export(), 
        this.setData(i);
    },
    handleSort: function(t) {
        var a = this, i = t.currentTarget.dataset, s = i.sort, r = i.sortName;
        s === this.data.curSort && r === this.data.curSortName || this.setData({
            curSort: s,
            curSortName: r,
            page: 1
        }, function() {
            a.handleSortAnim(!0);
            var t = {
                page: 1
            };
            r && (t.sort_name = 1 * r, t.sort = 1 * s), a.data.curClassify > 0 && (t.classify_id = a.data.curClassify), 
            a.getPictureList(t, !0);
        });
    },
    hideSortList: function() {
        var t = this, a = {}, i = this.data.curSortName, s = this.data.curClassify;
        a.sortType = null === i && s < 0 ? 0 : "2" === i ? 2 : "" === i || "1" === i ? 1 : s > -1 ? 3 : 0, 
        this.setData(a, function() {
            t.handleSortAnim(!0);
        });
    },
    handleClassify: function(t) {
        var a = this, i = 1 * t.currentTarget.dataset.id;
        i != this.data.curClassify && this.setData({
            curClassify: i,
            page: 1,
            curSort: null,
            curSortName: null
        }, function() {
            a.handleSortAnim(!0);
            var t = {
                page: 1,
                classify_id: i
            };
            a.getPictureList(t, !0);
        });
    }
});